<?php
/**
 *    @version [ Masterton ]
 *    @package hwdVideoShare
 *    @copyright (C) 2007 - 2009 Highwood Design
 *    @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 ***
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

global $sh_LANG, $sefConfig;

require_once(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS.'config.hwdvideoshare.php');
require_once(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS.'helpers'.DS.'initialise.php');
require_once(JPATH_SITE.DS.'components'.DS.'com_hwdvideoshare'.DS.'hwdvideoshare.class.php');
$c = hwd_vs_Config::get_instance();
hwdvsInitialise::language('plugs');
$db =& JFactory::getDBO();

if (eregi('Itemid=[0-9]+', $string) === false) {

	global $Itemid;
	$hwdvsItemid = hwd_vs_tools::generateValidItemid($Itemid);
	$Itemid = isset($Itemid) ? @$Itemid : $hwdvsItemid;

} else {

	$arr = parse_url($string);
	parse_str($arr["query"], $data);
	$Itemid = $data['Itemid'];

}

if (!function_exists('URLSafe')) {
	function URLSafe($string)
	{
		jimport( 'joomla.filter.output' );
		$string = JFilterOutput::stringURLSafe($string);

		/* All your other checks */
		return $string;
	}
}

// ------------------  standard plugin initialize function - don't change ---------------------------
$shLangName            = '';
$shLangIso             = '';
$title                 = array();
$shItemidString        = '';
$dosef                 = shInitializePlugin( $lang, $shLangName, $shLangIso, $option);
if ($dosef == false) return;

// remove common URL from GET vars list, so that they don't show up as query string in the URL
shRemoveFromGETVarsList('option');
shRemoveFromGETVarsList('lang');
if (!empty($Itemid))
  shRemoveFromGETVarsList('Itemid');
if (!empty($limit))
  shRemoveFromGETVarsList('limit');
if (isset($limitstart))
  shRemoveFromGETVarsList('limitstart'); // limitstart can be zero

// start by inserting the menu element title (just an idea, this is not required at all)
$task                  = isset($task) ? @$task : null;
$shSampleName          = shGetComponentPrefix($option);
$shSampleName          = empty($shSampleName) ?

getMenuTitle($option, $task, $Itemid, null, $shLangName) : $shSampleName;
$shSampleName = (empty($shSampleName) || $shSampleName == '/') ? 'VideoShare':$shSampleName;

if (empty($limit)) {
  $limit = $c->vpp;
  shAddToGETVarsList('limit', $limit);
  shRemoveFromGETVarsList('limit');
}

$title[] = URLSafe(_HWDVS_SEF_VIDEOS);

switch ($task) {
    case 'downloadfile':
    case 'downloadFile':
    case 'deliverthumb':
    case 'deliverThumb':
    case 'savegroup':
    case 'deletegroup':
    case 'joingroup':
    case 'leavegroup':
    case 'savevideo':
    case 'deletevideo':
    case 'setusertemplate':
    case 'publishvideo':
    case 'rate':
    case 'addfavourite':
    case 'removefavourite':
    case 'addvideotogroup':
    case 'reportvideo':
    case 'reportgroup':
    case 'ajax_rate':
    case 'ajax_addtofavourites':
    case 'ajax_removefromfavourites':
    case 'ajax_reportvideo':
    case 'ajax_addvideotogroup':
    case 'grabjomsocialplayer':
    case 'grabajaxplayer':
    case 'insertVideo':
	  $dosef = false;  // these tasks do not require SEF URL
	break;

	case 'frontpage':
		$title[] = URLSafe(_HWDVS_SEF_FP);
		shRemoveFromGETVarsList('task');
	break;

	case 'upload':
		$title[] = URLSafe(_HWDVS_SEF_UPLOAD);
		shRemoveFromGETVarsList('task');
	break;

	case 'uploadconfirmperl':
		$title[] = URLSafe(_HWDVS_SEF_UPLOADEDP);
		shRemoveFromGETVarsList('task');
	break;

	case 'uploadconfirmflash':
		$title[] = URLSafe(_HWDVS_SEF_UPLOADEDF);
		shRemoveFromGETVarsList('task');
	break;

	case 'uploadconfirmphp':
		$title[] = URLSafe(_HWDVS_SEF_UPLOADEDB);
		shRemoveFromGETVarsList('task');
	break;

	case 'addconfirm':
		$title[] = URLSafe(_HWDVS_SEF_ADDED);
		shRemoveFromGETVarsList('task');
	break;

	case 'groups':
		$title[] = URLSafe(_HWDVS_SEF_GROUPS);
		shRemoveFromGETVarsList('task');
	break;

	case 'creategroup':
		$title[] = URLSafe(_HWDVS_SEF_CREATEGROUP);
		shRemoveFromGETVarsList('task');
	break;

	case 'editgroup':
		$title[] = URLSafe(_HWDVS_SEF_EDITGROUP);
		shRemoveFromGETVarsList('task');
	break;

	case 'viewgroup':
		$title[] = URLSafe(_HWDVS_SEF_VIEWGROUP);
		shRemoveFromGETVarsList('task');
	break;

	case 'yourvideos':
		$title[] = URLSafe(_HWDVS_SEF_YV);
		shRemoveFromGETVarsList('task');
	break;

	case 'yourfavourites':
		$title[] = URLSafe(_HWDVS_SEF_YF);
		shRemoveFromGETVarsList('task');
	break;

	case 'yourgroups':
		$title[] = URLSafe(_HWDVS_SEF_YG);
		shRemoveFromGETVarsList('task');
	break;

	case 'yourmemberships':
		$title[] = URLSafe(_HWDVS_SEF_YM);
		shRemoveFromGETVarsList('task');
	break;

	case 'editvideo':
		$title[] = URLSafe(_HWDVS_SEF_EDITVIDEO);
		shRemoveFromGETVarsList('task');
	break;

	case 'featuredvideos':
		$title[] = URLSafe(_HWDVS_SEF_FEATUREDVIDEOS);
		shRemoveFromGETVarsList('task');
	break;

	case 'featuredgroups':
		$title[] = URLSafe(_HWDVS_SEF_FEATUREDGROUPS);
		shRemoveFromGETVarsList('task');
	break;

	case 'rss':
		$title[] = URLSafe(_HWDVS_SEF_RSS);
		shRemoveFromGETVarsList('task');
	break;

	case 'categories':
		$title[] = URLSafe(_HWDVS_SEF_CATEGORIES);
		shRemoveFromGETVarsList('task');
	break;

	case 'gotocategory':
		$menu = &JMenu::getInstance('site');
		$mparams = &$menu->getParams($Itemid);
		$hwdmcid = $mparams->get( 'hwdmcid', '');
    	$title[] = $hwdmcid;
    	$title[] = URLSafe(_HWDVS_SEF_VC);
	    shRemoveFromGETVarsList('task');
	break;

	case 'nextvideo':
		$title[] = URLSafe(_HWDVS_SEF_NV);
		shRemoveFromGETVarsList('task');
		$title[] = $category_id;
		shRemoveFromGETVarsList('category_id');
		$title[] = $video_id;
		shRemoveFromGETVarsList('video_id');
	break;

	case 'previousvideo':
		$title[] = URLSafe(_HWDVS_SEF_PV);
		shRemoveFromGETVarsList('task');
		$title[] = $category_id;
		shRemoveFromGETVarsList('category_id');
		$title[] = $video_id;
		shRemoveFromGETVarsList('video_id');
	break;

	case 'search':
		$title[] = URLSafe(_HWDVS_SEF_DR);
		shRemoveFromGETVarsList('task');
		if (!empty($category_id)) {
			$title[] = $category_id;
			shRemoveFromGETVarsList('category_id');
		}
		if (!empty($pattern)) {
			$title[] = $pattern;
			shRemoveFromGETVarsList('pattern');
		}
	break;

	case 'displayresults':
		$title[] = URLSafe(_HWDVS_SEF_DR);
		shRemoveFromGETVarsList('task');
		$title[] = $category_id;
		shRemoveFromGETVarsList('category_id');
		$title[] = $pattern;
		shRemoveFromGETVarsList('pattern');
	break;

	case 'viewvideo':
		$vid = intval($video_id);
		$sqlquery = 'SELECT v.title, c.category_name'
			   . ' FROM #__hwdvidsvideos AS v'
			   . ' LEFT JOIN #__hwdvidscategories AS c ON c.id = v.category_id'
			   . ' WHERE v.id = '.$vid
			   ;
		$db->SetQuery($sqlquery);
		$row = $db->loadObject();

		if (!isset($row->category_name)) { $row->category_name = ''; }
		if (!isset($row->title)) { $row->title = ''; }

		$categoryName 	= URLSafe(html_entity_decode($row->category_name));
		$videoName 	    = URLSafe(html_entity_decode($row->title));

		$title[] = URLSafe(_HWDVS_SEF_VIEWVIDEO);
		shRemoveFromGETVarsList('task');
		$title[] = $video_id;
		shRemoveFromGETVarsList('video_id');
		$title[] = $categoryName;
		$title[] = $videoName;
	break;

	case 'viewcategory':
		$cid = intval($cat_id);
		$sqlquery = 'SELECT c.category_name'
			   . ' FROM #__hwdvidscategories AS c'
			   . ' WHERE c.id = '.$cid
			   ;
		$db->SetQuery($sqlquery);
		$category_name = $db->loadResult();

		$categoryName 	= URLSafe(html_entity_decode($category_name));

		$title[] = URLSafe(_HWDVS_SEF_VIEWCATEGORY);
		shRemoveFromGETVarsList('task');
		$title[] = $cat_id;
		shRemoveFromGETVarsList('cat_id');
		$title[] = $categoryName;
	break;

	case 'viewgroup':
		$gid = intval($group_id);
		$sqlquery = 'SELECT g.group_name'
			   . ' FROM #__hwdvidsgroups AS g'
			   . ' WHERE g.id = '.$gid
			   ;
		$db->SetQuery($sqlquery);
		$group_name = $db->loadResult();

		$groupName 	= URLSafe(html_entity_decode($group_name));

		$title[] = URLSafe(_HWDVS_SEF_VIEWGROUP);
		shRemoveFromGETVarsList('task');
		$title[] = $group_id;
		shRemoveFromGETVarsList('$group_id');
		$title[] = $groupName;
	break;

	default:
		$title[] = URLSafe(_HWDVS_SEF_VIDEOS);
		shRemoveFromGETVarsList('task');
	break;
}

// ------------------  standard plugin finalize function - don't change ---------------------------
if ($dosef){
   $string = shFinalizePlugin( $string, $title, $shAppendString, $shItemidString,
      (isset($limit) ? @$limit : null), (isset($limitstart) ? @$limitstart : null),
      (isset($shLangName) ? @$shLangName : null));
}
// ------------------  standard plugin finalize function - don't change ---------------------------

?>
